<?php
namespace app\controller;

use app\BaseController;
use think\facade\Request;
use think\facade\Db;
use think\Exception;

class Location extends BaseController
{
    //查询所有位置
    public function getlocation()
    {
        $userId = $this->request->auth['data']->user->user_id;

        $current_page = Request::param('current_page');
        $page_size = Request::param('page_size');

        // 根据用户ID获取公司ID
        $companyId = Db::name('user')->where('user_id', $userId)->value('user_company_id');

        //分页查询所有资产类型
        $assetTypes = Db::table('location')
            ->where('location_company_id', $companyId)
            ->page($current_page, $page_size)
            ->select();

        //获取总条数
        $total = Db::table('location')
            ->where('location_company_id', $companyId)
            ->count();

        if (!$assetTypes) {
            return json([
                'code' => 400,
                'msg' => '查询失败',
                'data' => []
            ]);
        }
        return json([
            'code' => 200,
            'msg' => '查询成功',
            'data' => $assetTypes,
            'total' => $total
        ]);
    }

    //修改位置
    public function updatelocation()
    {
        $userId = $this->request->auth['data']->user->user_id;
        $id=Request::param('id');
        $data=Request::param('data');
        // 根据用户ID获取公司ID
        $companyId = Db::name('user')->where('user_id', $userId)->value('user_company_id');

        //没有包含switch字段说明是修改弹出框
        if (!isset($data['location_switch'])) {
            //判断除了当前资产名称外，是否存在相同资产名称
            $role_info = Db::table('location')
                ->where('location_id', '<>', $id)
                ->where('location_name', $data['location_name'])
                ->where('location_company_id', $companyId)
                ->find();

            if ($role_info) {
                return json([
                    'code' => 400,
                    'msg' => '位置名称已经存在',
                ]);
            }

        }
        //根据id修改资产类型,并开启事务
        try {
            Db::startTrans();
            Db::table('location')->where('location_id', $id)->update($data);
            Db::commit();
            return json([
                'code' => 200,
                'msg' => '修改成功',
                'data' => []
            ]);
        } catch (Exception $e) {
            Db::rollback();
            return json([
                'code' => 400,
                'msg' => '修改失败',
            ]);


        }


    }
    //添加位置
    public function addlocation()
    {
        $userId = $this->request->auth['data']->user->user_id;
        $data=Request::param('data');
        $companyId = Db::name('user')->where('user_id', $userId)->value('user_company_id');

        //根据data中的AssetTypes_name查询是否存在相同资产类型，如果存在则提示
        $info = Db::table('location')
            ->where('location_name', $data['location_name'])
            ->where('location_company_id', $companyId)
            ->find();
        if ($info) {
            return json([
                'code' => 400,
                'msg' => '位置已经存在',
            ]);
        }
        $data['location_company_id']=$companyId;
        try {
            Db::startTrans();
            Db::table('location')->insert($data);
            Db::commit();
            return json([
                'code' => 200,
                'msg' => '添加成功',
            ]);
        }
        catch (Exception $e) {
            Db::rollback();
            return json([
                'code' => 400,
                'msg' => '添加失败',
            ]);
        }


    }

    //删除位置
    public function deletelocation()
    {
        $userId = $this->request->auth['data']->user->user_id;
        $id=Request::param('id');
        // 根据用户ID获取公司ID
        $companyId = Db::name('user')->where('user_id', $userId)->value('user_company_id');

        //根据id查询位置是否存在，不存在则提示
        $info = Db::table('location')
            ->where('location_id', $id)
            ->where('location_company_id', $companyId)
            ->find();
        if (!$info) {
            return json([
                'code' => 400,
                'msg' => '位置不存在',
            ]);
        }
        try {
            Db::startTrans();
            Db::table('location')->where('location_id', $id)->delete();
            Db::commit();
            return json([
                'code' => 200,
                'msg' => '删除成功',
            ]);
        }
        catch (Exception $e) {
            Db::rollback();
            if($e->getCode()==10501)
            {
                return json(['code' => 500, 'msg' => '该位置下已存在单据，无法删除']);
            }
            return json([
                'code' => 400,
                'msg' => '删除失败',
            ]);
        }
    }
}